/**
 * 
 */
package com.lmx.persistence.service;

import java.util.LinkedList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.common.BaseCrudServiceImpl;
import com.lmx.repoDomain.tmodel.Employee;
import com.lmx.repoDomain.tmodel.EmployeeProformance;

/**
 * 员工业绩奖罚Service实现
 * @author  djs
 * 2018年4月3日
 */
@Service
public class EmployeeProformanceServiceImpl extends BaseCrudServiceImpl<EmployeeProformance> implements EmployeeProformanceService{
	private Log logger = LogFactory.getLog(this.getClass());
	@Autowired
	private EmployeeService employeeService;
	@SuppressWarnings("unchecked")
	@Override
	public LinkedList getEmployeeRelativeEmployeeProformance(List<Employee> employees) {
		List<Long> employIds = employeeService.sqlInParam(employees);
		Integer startTime = null;
		Integer endTime = null;
		try {
			startTime = CommonUtil
					.strToIntegerTime(CommonUtil.getPreMonthFirstDayForString().substring(0, 10) + " 00:00:00");
			endTime = CommonUtil
					.strToIntegerTime(CommonUtil.getMonthFirstDayForString().substring(0, 10) + " 00:00:00");
		} catch (Exception e) {
			logger.debug("getEmployeeRelativeEmployeeProformance error :" + e);
		}
		//员工绩效信息数据（为每个员工奖励、惩罚统计分组，并以员工ID排序）
		StringBuilder epSql = new StringBuilder("select ep.employee.id,ep.type.typecode,SUM(ep.amount) from EmployeeProformance ep where ep.employee.id in(:ids) and (ep.createTime between ?2 and ?3) GROUP BY ep.employee.id,ep.type.typecode order by ep.employee.id");
		List objects = this.em.createQuery(epSql.toString()).setParameter("ids", employIds).setParameter(2, startTime).setParameter(3, endTime).getResultList();
		LinkedList<Object> objectss = new LinkedList<>();
		objectss.addAll(objects);
		return objectss;
	}
}
